Ext.BLANK_IMAGE_URL='/wsd/image/page/s.gif';
Ext.onReady(function(){
	var parent_id = 0;//默认类别为父类别
	var parent_level = 0;
	var c_node = null;
	var categoryTree = new Ext.tree.TreePanel({
		title:'业务',
		region:'west',
		frame:true,
		width:200,
		split:true,
		collapsible:true,
		rootVisible : false,
		autoScroll : true,
		
		root:new Ext.tree.AsyncTreeNode({
			id : '0',
			text : '类别'
		}),
		loader: new Ext.tree.TreeLoader({
			url:'/wsd/admin/category_categoryTreeData.action'
    	})
	});
	categoryTree.expandAll();
	var categoryName = new Ext.form.Label({
		fieldLabel:'类别',
		allowBlank:false,
		name:'category.CName'
	})
	var productNameField = new Ext.form.TextField({
		fieldLabel:'商品名称(*)',
		allowBlank:false,
		blankText:'商品名称不能为空',
		msgTarget:'under',
		width:200,
		name:'product.PName'
	})
	
	var barcodeField = new Ext.form.TextField({
		fieldLabel:'条形码(*)',
		allowBlank:false,
		blankText:'条形码不能为空',
		msgTarget:'under',
		width:200,
		name:'product.barcode'
	})
	var modelField = new Ext.form.TextField({
		fieldLabel:'型号(*)',
		allowBlank:false,
		blankText:'型号不能为空',
		msgTarget:'under',
		name:'product.model'
	})
	var factoryField = new Ext.form.TextField({
		fieldLabel:'厂商',
		width:200,
		name:'product.factory'
	})
	
	var descriptionArea = new Ext.form.HtmlEditor({
		fieldLabel:'描述',
		width:400,
		height:200,
		name:'product.description'
	})
	var categoryField = new Ext.form.Hidden({
		name:'category.CId'
	})
	
	var userField1 = new Ext.form.NumberField({
		width:50,
		allowBlank:false,
		blankText:'普通会员价格不能为空',
		msgTarget:'under',
		name:'price1.price',
		fieldLabel:'普通会员'
	})
	var userField2 = new Ext.form.NumberField({
		name:'price2.price',
		width:50,
		fieldLabel:'高级会员'
	})
	var userField3 = new Ext.form.NumberField({
		width:50,
		name:'price3.price',
		fieldLabel:'VIP会员'
	})
	userField1.on('blur',function(field){
		var price = field.getValue();
		userField2.setValue(price);
		userField3.setValue(price);
	});
	var pricePanel = new Ext.Panel({
		fieldLabel:'价格',
		layout:'column',
		items:[
			{
				columnWidth:.3,
				layout:'form',
				items:[userField1]
			},
			{
				columnWidth:.3,
				layout:'form',
				items:[userField2]
			},
			{
				columnWidth:.3,
				layout:'form',
				items:[userField3]
			}
		]
	})
	var addBtn = new Ext.Button({
		text:'添加',
		handler:addProduct
		
	})
	var resetBtn = new Ext.Button({
		text:'重置',
		handler:function(){
			
			var categoryText = categoryName.text;
			var categoryId = categoryField.getValue();
			form.getForm().reset();
			categoryField.setValue(categoryId);
			categoryName.setText(categoryText);
		}
	})
	var form = new Ext.form.FormPanel({
		region:'center',
		frame:'true',
		fileUpload:true,
		items:[categoryName,productNameField,barcodeField,modelField,factoryField,{
            xtype: 'fileuploadfield',
            name:'upload',
            allowBlank:false,
            blankText:'图片不能为空',
			msgTarget:'under',
            id: 'upload_form',
            width:400,
            emptyText: '请选择图片文件',
            fieldLabel: '图片(*)',
            buttonText: '',
            buttonCfg: {
                iconCls: 'upload-icon'
            }
        },pricePanel,descriptionArea,categoryField
		],
		buttons: [addBtn,resetBtn]
	})
	
	var viewport = new Ext.Viewport({
		layout : 'border',
		frame:true,
		items : [categoryTree,form]
	});
	categoryTree.on("click",function(node,e){
		parent_id = node.attributes.id;
		parent_level = node.attributes.level;
		categoryField.setValue(parent_id);
		categoryName.setText(node.attributes.text)
	})
	/**
	 * 向后台提交新添加的类别
	 */
	function send(categories){
		Ext.Msg.wait("正在添加……");
		Ext.Ajax.request({
			url:'/wsd/admin/category_addProduct.action',
			params:{
				cNames:categories,
				'parent.CLevel':parent_level,	
				'parent.CId':parent_id
			},
			
			success:function(response){
				var result = Ext.util.JSON.decode(response.responseText).result;
				if(!result.addResult){
					Ext.Msg.alert("系统信息","添加失败");
					return;
				}
				var categories = result.categories;
				var pNode = categoryTree.getNodeById(parent_id);
				var len = categories.length;
				pNode.attributes.leaf = false;
				for(var i = 0; i < len; i++){
					var categorie = categories[i];
					var node = new Ext.tree.TreeNode({
						id:categorie.CId,
						text:categorie.CName,
						leaf:true,
						level:categorie.CLevel
				})
					pNode.appendChild(node)
				}
				categoryTree.expandAll();
				Ext.Msg.hide();
				Ext.Msg.alert("系统信息","添加" + result.addSize + "条记录");
			},
			failure:function(response){
				Ext.Msg.alert('系统消息','添加失败');
			}
		})
	}

	function addProduct(){
		
		if(categoryField.getValue() == ''){
			Ext.Msg.alert('系统信息','请先选择类别');
			return;
		}
		
		if(!validateFile(Ext.getCmp('upload_form').getValue())){
    		return false;
    	}
    	if(form.getForm().isValid()){
            form.getForm().submit({
            	url:'admin/product_addProduct.action',
                waitMsg: '系统正在处理，请稍后……',
                success: function(fp, o){
                	Ext.Msg.alert('系统信息',"添加成功");
                	//addBtn.setDisabled(true);
                	//resetBtn.setText('继续添加');
                	
                	
                },
                failure:function(fp, o){
                	Ext.Msg.alert('系统信息',o.result.msg);
                }
            });
        }
	}
	/***********************函数*****************************/
	function validateFile(file){
		if(file == '' || file == null){
			Ext.Msg.alert('系统信息','请选择文件');
			return false;
		}
		var req = /(.jpg|.jpeg|.bmp|.png|.gif|.JPG|.JPEG|.BMP|.PNG|.GIF)$/;
		var result = req.exec(file);
		if(result == null){
			Ext.Msg.alert('系统信息','文件格式错误，请上传图片文件!');
			return false;
		}
		return true;
	}
});